package com.ycsj.goldmedalnewconcept.fragment;

import java.io.IOException;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.adapter.LazyBaseFragment;
import com.ycsj.goldmedalnewconcept.bean.CourseInfo;
import com.ycsj.goldmedalnewconcept.bean.Huodong;
import com.ycsj.goldmedalnewconcept.bean.TeacherManageInfo;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

import android.media.Image;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TeacherInfoFragment extends LazyBaseFragment {
	private LinearLayout llLoading;
	private ImageView iv_img;
	private TextView tv_name;
	private TextView tv_sex;
	private TextView tv_birthday;
	private TextView tv_school;
	private TextView tv_jiaofeibili;
	private TextView tv_chuqinlv;
	private TextView tv_score;
	private String user_id;

	@Override
	protected int getLayoutId() {
		return R.layout.fragment_teacherinfo;
	}

	@Override
	public void lazyInit(View view, Bundle savedInstanceState) {

		// 加载控件
		llLoading = (LinearLayout) view.findViewById(R.id.ll_loading);
		iv_img = (ImageView) view.findViewById(R.id.iv_img);
		tv_name = (TextView) view.findViewById(R.id.tv_name);
		tv_sex = (TextView) view.findViewById(R.id.tv_sex);
		tv_birthday = (TextView) view.findViewById(R.id.tv_birthday);
		tv_school = (TextView) view.findViewById(R.id.tv_school);
		tv_jiaofeibili = (TextView) view.findViewById(R.id.tv_jiaofeibili);
		tv_chuqinlv = (TextView) view.findViewById(R.id.tv_chuqinlv);
		tv_score = (TextView) view.findViewById(R.id.tv_score);

		
		user_id = (String) getArguments().getSerializable("user_id");
		
		
		initData();

		
	}

	private void initData() {
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "28");
		builder.add("c", user_id);
		builder.add("role", "2");
		// http://pm.moregolden.com/ycsj_count/MessageCenter?type=28&c=13006314760&role=2
		Request request = new Request.Builder()
				.url(Constant.ycsj_count_MessageCenter).post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {

			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();
				String Json = arg0.body().string();
				Log.e("test", Json);
				//http://118.190.89.38:8088/ycsj_count/MessageCenter?type=28&c=18988888813&role=2
//				final TeacherManageInfo fromJson = null;
				final TeacherManageInfo fromJson = gson.fromJson(Json,
						TeacherManageInfo.class);
				
				if(fromJson!=null){
					
				try {
					getActivity().runOnUiThread(new Runnable() {
						@Override
						public void run() {
							llLoading.setVisibility(llLoading.GONE);
							Picasso.with(getActivity())
							.load(fromJson.icon)
							.error(R.drawable.header).into(iv_img);
							
							
							tv_name.setText(fromJson.name);
							if("1".equals(fromJson.sex)){
								tv_sex.setText("性别：男");
							}
							if("0".equals(fromJson.sex)){
								tv_sex.setText("性别：女");
							}
							tv_birthday.setText("出生年月日："+fromJson.birthday);
							tv_school.setText("所在校区："+fromJson.school);
							tv_jiaofeibili.setText("学生总缴费比例："+fromJson.payment);
							tv_chuqinlv.setText("学生平均出勤率："+fromJson.averageAttendance);
							tv_score.setText("学生平均成绩："+fromJson.averageExamResult);
							
							
						}
					});
				} catch (Exception e) {
					Log.e("test", "错误");
				}
				}
			}
		});
	}
}
